{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 离散化分箱操作",
   "id": "baa7893f442815cc"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-15T08:03:29.576628Z",
     "start_time": "2025-09-15T08:03:29.145613Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day16/file/'"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1 指定分界点分箱",
   "id": "db1f09d59a12c1c3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:10:24.838188Z",
     "start_time": "2025-09-15T08:10:24.828915Z"
    }
   },
   "cell_type": "code",
   "source": [
    " years = [1992, 1983, 1922, 1932, 1973]\n",
    " box = [1900,1950,2000] #指定箱子的分界点，20世纪上半页，二十世纪下半页\n",
    "\n",
    " result = pd.cut(years,box,labels=['上半页','下半页'])\n",
    " # print(result)\n",
    "\n",
    " print(result.value_counts()) # 统计每个箱子重有几个数据\n",
    " # print(pd.Series(result).value_counts())"
   ],
   "id": "55faa003c3efd6e0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下半页    3\n",
      "上半页    2\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:12:27.220904Z",
     "start_time": "2025-09-15T08:12:27.215597Z"
    }
   },
   "cell_type": "code",
   "source": [
    " years = [1992, 1983, 1922, 1932, 1973]\n",
    " box = [1900,1950,2000] #指定箱子的分界点，20世纪上半页，二十世纪下半页\n",
    "\n",
    " result = pd.cut(years,box,labels=False) # 如果label是False用下标标识\n",
    " print(result)\n",
    "\n",
    " print(pd.Series(result).value_counts()) # 统计每个箱子重有几个数据"
   ],
   "id": "e0d587ae19ff1b69",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 0 0 1]\n",
      "1    3\n",
      "0    2\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 等频分箱 qcut",
   "id": "81337e010dba5729"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:15:42.353570Z",
     "start_time": "2025-09-15T08:15:42.344594Z"
    }
   },
   "cell_type": "code",
   "source": [
    "years = [1992, 1983, 1922, 1932, 1973, 1999, 1993, 1995]\n",
    "result = pd.qcut(years,4)\n",
    "print(result)\n",
    "print(pd.Series(result).value_counts())"
   ],
   "id": "3b79c458f15c116a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1987.5, 1993.5], (1962.75, 1987.5], (1921.999, 1962.75], (1921.999, 1962.75], (1962.75, 1987.5], (1993.5, 1999.0], (1987.5, 1993.5], (1993.5, 1999.0]]\n",
      "Categories (4, interval[float64, right]): [(1921.999, 1962.75] < (1962.75, 1987.5] < (1987.5, 1993.5] < (1993.5, 1999.0]]\n",
      "(1921.999, 1962.75]    2\n",
      "(1962.75, 1987.5]      2\n",
      "(1987.5, 1993.5]       2\n",
      "(1993.5, 1999.0]       2\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:18:17.392213Z",
     "start_time": "2025-09-15T08:18:17.383197Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 指定分位数\n",
    "years = [1992, 1983, 1922, 1932, 1973, 1999, 1993, 1995]\n",
    "quantile = [0.25,0.5,0.75,1] # 指定分位数\n",
    "result = pd.qcut(years,quantile)\n",
    "print(result)\n"
   ],
   "id": "4e0635a519523c56",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1987.5, 1993.5], (1962.749, 1987.5], NaN, NaN, (1962.749, 1987.5], (1993.5, 1999.0], (1987.5, 1993.5], (1993.5, 1999.0]]\n",
      "Categories (3, interval[float64, right]): [(1962.749, 1987.5] < (1987.5, 1993.5] < (1993.5, 1999.0]]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 实际案例，按照年龄分组",
   "id": "d9e096ca8265df6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:22:54.709717Z",
     "start_time": "2025-09-15T08:22:54.701125Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "np.random.seed(42)\n",
    "ages = np.random.randint(0,100,100)\n",
    "print(ages)\n",
    "# 按照年龄分组\n",
    "box = [0,15,30,45,60,75,100]\n",
    "result = pd.cut(ages,box,labels=['少年','青年','壮年','中年','暮年','耄耋'])\n",
    "print(result)\n",
    "print(result.value_counts())"
   ],
   "id": "7e97b9da1317340",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[51 92 14 71 60 20 82 86 74 74 87 99 23  2 21 52  1 87 29 37  1 63 59 20\n",
      " 32 75 57 21 88 48 90 58 41 91 59 79 14 61 61 46 61 50 54 63  2 50  6 20\n",
      " 72 38 17  3 88 59 13  8 89 52  1 83 91 59 70 43  7 46 34 77 80 35 49  3\n",
      "  1  5 53  3 53 92 62 17 89 43 33 73 61 99 13 94 47 14 71 77 86 61 39 84\n",
      " 79 81 52 23]\n",
      "['中年', '耄耋', '少年', '暮年', '中年', ..., '耄耋', '耄耋', '耄耋', '中年', '青年']\n",
      "Length: 100\n",
      "Categories (6, object): ['少年' < '青年' < '壮年' < '中年' < '暮年' < '耄耋']\n",
      "少年    18\n",
      "青年    10\n",
      "壮年    10\n",
      "中年    21\n",
      "暮年    16\n",
      "耄耋    25\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 17
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
